GithubAppAPI   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 9

6 Functions

Rating   Name   Duplication   Size   Complexity  
A refreshToken 0 11 2
A createToken 0 13 1
A listInstallations 0 5 2
A constructor 0 10 1
A getHeaders 0 6 1
A createAcessToken 0 5 1
1
import BaseAPI from 'base-api-client';
2
import dayjs from 'dayjs';
3
import jsonwebtoken from 'jsonwebtoken';
4
5
function tokenTimestamp(date) {
6
    return Math.floor(date.unix());
7
}
8
9
export default class GithubAppAPI extends BaseAPI {
10
    constructor(ghAppConfig) {
11
        const { tokenExpire, privateKey, appID, timeToRefresh } = ghAppConfig;
12
13
        super('https://api.github.com/app');
14
        this._tokenExpire = tokenExpire;
15
        this._privateKey = privateKey;
16
        this._appID = appID;
17
        this._timeToRefresh = timeToRefresh;
18
        this.createToken();
19
    }
20
21
    createToken() {
22
        const payload = {
23
            exp : tokenTimestamp(dayjs().add(this._tokenExpire)),
24
            iat : tokenTimestamp(dayjs().subtract(1, 'minute')),
25
            iss : this._appID
26
        };
27
28
        this.token = jsonwebtoken.sign(
29
            payload,
30
            this._privateKey,
31
            { algorithm: 'RS256' }
32
        );
33
    }
34
35
    refreshToken() {
36
        const payload = jsonwebtoken.decode(this.token);
37
        const expTime = dayjs.unix(payload.exp);
38
        const left = expTime.diff(dayjs(), 'millisecond');
39
40
        if (left < this._timeToRefresh) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if left < this._timeToRefresh is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
41
            this.createToken();
42
43
            return true;
44
        }
45
    }
46
47
    getHeaders() {
48
        return {
49
            Authorization : `Bearer ${this.token}`,
50
            Accept        : 'application/vnd.github.v3+json'
51
        };
52
    }
53
54
    async listInstallations() {
55
        const installations = await this.get('/installations');
56
57
        return installations.map(i => dumpInstallation(i));
58
    }
59
60
    async createAcessToken(installationId) {
61
        const token =  await this.post(`/installations/${installationId}/access_tokens`);
62
63
        return dumpAccessToken(token);
64
    }
65
}
66
67
function dumpInstallation(i) {
68
    return { id: i.id };
69
}
70
71
function dumpAccessToken(t) {
72
    return {
73
        token : t.token
74
    };
75
}
76